import { h } from 'preact';

import { Rect, getRectAnchors } from './basic-shape/Rect'
import { Circle, getCircleAnchors } from './basic-shape/Circle'

import RectModel from './model/RectModel';
import CircleModel from './model/CircleModel';

const shapeMap = new Map();

shapeMap.set('rect', {
  component: Rect,
  getAnchors: getRectAnchors,
  model: RectModel
});

shapeMap.set('circle', {
  component: Circle,
  getAnchors: getCircleAnchors,
  model: CircleModel
});

export function getShapeComponent (type) {
  const shape = shapeMap.get(type);
  if (!shape) {
    throw new Error('未注册的类型')
  }
  return shape.component;
}

export function getShapeAnchor(props) {
  const shape = shapeMap.get(props.type);
  if (!shape) {
    throw new Error('未注册的类型')
  }
  return shape.getAnchors(props);
}

export function getShapeModel(type) {
  const shape = shapeMap.get(type);
  if (!shape) {
    throw new Error('未注册的类型')
  }
  return shape.model;
}

export function setShape (type, shapeConfig) {
  return shapeMap.set(type, shapeConfig)
}
